package hu.szyrt.bug.connections;

import hu.szyrt.bug.utils.Config;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginConnection extends BaseConnection {
	
	private int memberID = -1;
	private String resultText = null;
	
	public LoginConnection() {
		super(Config.URL_BUG_LOGIN);		
	}
	
	@Override
	public void run() {
		
		parameterizedURL();		
		if(download()){
			// JSON decode
			try {
				JSONObject jsonResult = new JSONObject(rawResults);
				this.resultText = jsonResult.getString("result_text");
				this.memberID = jsonResult.getInt("member_id");				
				if (jsonResult.getBoolean("result")) postSuccess();
				else 								 postError();							
			} catch (JSONException e) {
				e.printStackTrace();
				postError();
			}
		}else{
			postError();
		}		
	}
	
	
	// Setters --------------------------	
	public void setEmail(String email){
		this.params.put("email", email);		
	}
	
	public void setPassword(String password){
		this.params.put("password", salterize(password));
	}
	
	
	// Getters --------------------------
	public int getMemberID() {
		return memberID;
	}
	
	public String getEmail(){
		return this.params.get("email");
	}
	
	public String getResultText() {
		return resultText;
	}
}
